// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.wizards;

import java.util.HashMap;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.metadata.MetadataHelper;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.DatabaseFactory;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.IPath;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-29 qiang.zhang $
 * 
 */
public abstract class AbstractElementWizard extends AbstractStorageWizard {

    protected IPath path;

    protected DatabaseDiagram database;

    protected NewElementWizardPage1 page1;

    protected NewElementWizardPage2 page2;

    protected NewElementWizardPage3 page3;

    protected boolean isActived;

    protected ENodeCategoryName name;

    public AbstractElementWizard(DatabaseDiagram database, IPath path, boolean isActived, ENodeCategoryName name) {
        super();
        this.database = database;
        this.path = path;
        this.isActived = isActived;
        this.name = name;
        setWindowTitle(Messages.getString("NewXmlFileWizardPage.Title", name.getDisplayName())); //$NON-NLS-1$
        setDefaultPageImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.WAREHOUSE_WIZARD_DATABASE));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
        if (database == null) {
            page1 = new NewElementWizardPage1(path, name);
        } else {
            page1 = new NewElementWizardPage1(database, isActived, name);
        }
        page2 = newElementWizardPage2(path, database, name);
        page3 = newElementWizardPage3(path, database, name);
        addPage(page1);
        addPage(page2);
        addPage(page3);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newXmlWizardPage3".
     * 
     * @param path2
     * @return
     */
    protected abstract NewElementWizardPage3 newElementWizardPage3(IPath path2, DatabaseDiagram database, ENodeCategoryName name);

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newXmlWizardPage2".
     * 
     * @param path2
     * @param database
     * @return
     */
    protected abstract NewElementWizardPage2 newElementWizardPage2(IPath path2, DatabaseDiagram database, ENodeCategoryName name);

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    public boolean performFinish() {
        boolean create = false;
        // boolean rename = false;
        String databaseName = page1.getDatabaseName();
        if (database == null) {
            database = DatabaseFactory.eINSTANCE.createDatabaseDiagram();
            create = true;
            database.setUuid(WarehouseResourceFactory.getEMFId());
        }
        // else if (path != null) {
        // if (!database.getName().equals(databaseName)) {
        // rename = true;
        // WarehouseResourceFactory.deleteObjectDataPhysical(database);
        // }
        // }
        database.setCategoryName(name.getName());
        database.setComment(page1.getDatabaseComment());
        database.setName(databaseName);
        MetadataHelper.fixedTablesLocation(database, page3.getDbtable());
        HashMap<String, Object> properties = page3.getProperties();
        database.setProperties(properties);
        database.setStatus(page1.getDatabaseStatus());
        database.setEmfType(name.getEMFDataType());
        if (create) {
            WarehouseResourceFactory.createStorage(database, path);
        } else {
            WarehouseResourceFactory.saveStorage(database);
        }
        return true;
    }
}
